package com.merge.sample;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.merge.sample.model.MData;
import com.merge.sample.util.MyUtil;

public class DefaultFolderMerge implements CanMerge {
	
	private String projectName = "";
	
	public DefaultFolderMerge(String projectName) {
		this.projectName = projectName;
	}

	@Override
	public List<String> checkConflict(String source, String target, MData data) {
		return MyUtil.checkConflictFiles(source, target, data);
	}

	@Override
	public String doMerge(String source, String target, MData data) {
		String message = null;
		Set<String> ignoreList = new HashSet<String>();
		String[] sourceFiles = MyUtil.listFile(source);
		for(String sf : sourceFiles) {
			if(data.isIgnore(sf)) {
				ignoreList.add(sf);
			}
		}
		try {
			int num = MyUtil.copyFiles(source, target, ignoreList);
			message = String.format("Merge success, merged %s files", num);
		} catch (IOException e) {
			e.printStackTrace();
			message = "Some error has happened when merge";
		}
		return message;
	}

	@Override
	public void setInfo(String info) {

	}

}
